home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.09 Sep 87 / show clip source / showclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-06  |  6.4 KB  |  300 lines  |  [TEXT/KAHL]

  1. #include    <abc.h>
  2.  
  3. #include    <MacTypes.h>
  4. #include    <Quickdraw.h>
  5. #include    <WindowMgr.h>
  6. #include    <EventMgr.h>
  7. #include    <MenuMgr.h>
  8. #include    <DeskMgr.h>
  9. #include    <ScrapMgr.h>
  10.  
  11. #define        AppleID    30
  12. #define        FileID    31
  13. #define        EditID    32
  14.  
  15. MenuHandle    appleMenu,
  16.             fileMenu,
  17.             editMenu;
  18. EventRecord    theEvent;
  19. WindowPtr    clipWindow;
  20.  
  21. short        scrapCompare;
  22. char        theString[256];
  23. Boolean        done;
  24.  
  25. main()
  26. {
  27.     initialize();
  28.     doEvents();
  29. } /* main */
  30.  
  31. initialize()
  32. {
  33.     Rect    wRect;
  34.  
  35.     InitGraf(&thePort);
  36.     InitFonts();
  37.     InitWindows();
  38.     InitMenus();
  39.     TEInit();
  40.     InitDialogs( NULL );
  41.     InitCursor();
  42.     
  43.     FlushEvents ( everyEvent, 0 );
  44.     setUpMenus();
  45.     
  46.     SetRect ( &wRect, 60, 50, 450, 250 );
  47.     clipWindow = NewWindow( NULL, &wRect, "\PClipboard",
  48.          TRUE, 0, -1L, FALSE, NULL );
  49.  
  50.     /* force an initial update of the Clipboard window */
  51.     scrapCompare = InfoScrap()->scrapCount + 1;
  52.     
  53.     done = FALSE;
  54.  
  55. } /* initialize */
  56.  
  57. setUpMenus()
  58. {
  59.     appleMenu = NewMenu ( AppleID, "\P\024" );
  60.     AddResMenu ( appleMenu, 'DRVR');
  61.     InsertMenu ( appleMenu, 0 );
  62.  
  63.     fileMenu = NewMenu ( FileID, "\PFile" );
  64.     AppendMenu ( fileMenu, "\PQuit" );
  65.     InsertMenu ( fileMenu, 0 );
  66.  
  67.     editMenu = NewMenu ( EditID, "\PEdit" );
  68.     AppendMenu ( editMenu, "\PUndo;(-;Cut;Copy;Paste;Clear" );
  69.     InsertMenu ( editMenu, 0 );
  70.     
  71.     DrawMenuBar();
  72.     
  73. } /* setUpMenus */
  74.  
  75. doEvents()
  76. {
  77.     WindowPtr    wWindow;
  78.     GrafPtr        savePort;
  79.     INTEGER        thePart;
  80.     
  81.     done = FALSE;
  82.     do {
  83.     
  84.         /* check to see if the scrap has changed recently… */
  85.         if ( scrapCompare NEQ InfoScrap()->scrapCount ) {
  86.             GetPort ( &savePort );
  87.             SetPort ( clipWindow );
  88.             InvalRect ( &clipWindow->portRect );
  89.             SetPort ( savePort );
  90.             scrapCompare = InfoScrap()->scrapCount;
  91.         }
  92.         
  93.         if ( GetNextEvent ( everyEvent, &theEvent ) ) {
  94.             switch ( theEvent.what ) {
  95.             case mouseDown:
  96.                 thePart = FindWindow ( theEvent.where, &wWindow );
  97.                 switch ( thePart ) {
  98.                 case inContent:
  99.                     if ( FrontWindow() NEQ wWindow )
  100.                         SelectWindow ( wWindow );
  101.                     break;
  102.                 case inMenuBar:
  103.                     doMenuClick();
  104.                     break;
  105.                 case inSysWindow:
  106.                     SystemClick ( &theEvent, wWindow );
  107.                     break;
  108.                 default:
  109.                     SysBeep ( 2 );
  110.                     break;
  111.                 } /* switch thePart */
  112.                 break;
  113.             case keyDown:
  114.                 done = TRUE;
  115.                 break;
  116.             case updateEvt:
  117.                 if ( (WindowPtr) theEvent.message EQ clipWindow )
  118.                     updateClipWindow ( clipWindow );
  119.                 break;
  120.             case activateEvt:
  121.                 if ( (WindowPtr) theEvent.message EQ clipWindow ) {
  122.                     SetPort ( theEvent.message );
  123.                     if ( theEvent.modifiers bAND activeFlag )
  124.                         DisableItem ( editMenu, 0 );
  125.                     else
  126.                         EnableItem ( editMenu, 0 );
  127.                 }
  128.                 break;
  129.             } /* switch theEvent.what */
  130.         } /* if GNE */
  131.     } while ( NOT done );
  132. } /* doEvents */
  133.  
  134. doMenuClick()
  135. {
  136.     long menuChoice;
  137.  
  138.     menuChoice = MenuSelect ( theEvent.where );
  139.     doMenuChoice ( menuChoice );
  140.     
  141. } /* doMenuClick */
  142.  
  143. doMenuChoice( theMenu, theItem )
  144. short theMenu, theItem;
  145. {
  146.     short    accNumber;
  147.     
  148.     switch ( theMenu ) {
  149.     case AppleID:
  150.         GetItem ( appleMenu, theItem, theString );
  151.         accNumber = OpenDeskAcc ( theString );
  152.         break;
  153.     case FileID:
  154.         if ( theItem EQ 1 )
  155.             done = TRUE;
  156.         break;
  157.     case EditID:
  158.         if ( NOT SystemEdit ( theItem - 1) )
  159.             SysBeep ( 2 );
  160.         break;
  161.     default:
  162.         break;
  163.     } /* switch */
  164.     
  165.     HiliteMenu ( 0 );
  166.         
  167. } /* doMenuChoice */
  168.  
  169. updateClipWindow ( badWindow )
  170. WindowPtr    badWindow;
  171. {
  172.     GrafPtr    savePort;
  173.     Rect    dispRect;
  174.     Handle    theHandle;
  175.     long    offset,
  176.             theSize;
  177.     
  178.     theHandle = NewHandle ( 8L );
  179.     
  180.     GetPort ( &savePort );
  181.     SetPort ( badWindow );
  182.     BeginUpdate ( badWindow );
  183.         EraseRect ( &badWindow->portRect );
  184.         
  185.         dispRect = badWindow->portRect;
  186.         InsetRect ( &dispRect, 5, 5 );
  187.         
  188.         if ( (theSize = GetScrap ( theHandle,
  189.                             'TEXT', &offset )) > 0 ) {
  190.             HLock ( theHandle );
  191.             myTextBox ( badWindow, &dispRect,
  192.                                 *theHandle, theSize );
  193.             HUnlock ( theHandle );
  194.         } else if ( (theSize = GetScrap ( theHandle,
  195.                             'PICT', &offset )) > 0 ) {
  196.             drawAPicture ( badWindow, &dispRect,
  197.                                 theHandle );
  198.         }
  199.     EndUpdate ( badWindow );
  200.     
  201.     DisposHandle ( theHandle );
  202.     
  203. } /* updateClipWindow */
  204.  
  205. myTextBox ( textWindow, textRect, textPtr, textLength )
  206. WindowPtr        textWindow;
  207. Rect            *textRect;
  208. register char    textPtr[];
  209. long            textLength;
  210. {
  211.     register long     index;
  212.     
  213.     FontInfo        theFontInfo;
  214.     short            textHeight,
  215.                     boxWidth, boxHeight,
  216.                     theHeight, strLength;
  217.     long            startLine, canBreak;
  218.                 
  219.     TextFont ( 3 );        /* Geneva */
  220.     TextSize ( 12 );    /* 12 point */
  221.     TextFace ( 0 );        /* Plain Text */
  222.  
  223.     GetFontInfo ( &theFontInfo );
  224.     textHeight = theFontInfo.ascent + theFontInfo.descent
  225.                     + theFontInfo.leading;
  226.     
  227.     boxWidth = textRect->right - textRect->left;
  228.     boxHeight = textRect->bottom - textRect->top;
  229.  
  230.     theHeight = textRect->top - theFontInfo.descent;
  231.     strLength = 0;
  232.     startLine = canBreak = -1;
  233.  
  234.     for ( index = 0; index < textLength AND theHeight < boxHeight; index++ ) {
  235.         if ( textPtr[index] EQ CR ) /* if current char is a CR… */ {
  236.             theHeight += textHeight;
  237.             MoveTo ( textRect->left, theHeight );
  238.             DrawText ( textPtr, startLine+1,
  239.                                 index-startLine-1 );
  240.             strLength = 0;
  241.             startLine = canBreak = index;
  242.         } else /* check for word wrap */ {
  243.             strLength += CharWidth ( textPtr[index] );
  244.             if ( strLength > boxWidth ) {
  245.                 if ( canBreak > startLine )
  246.                     index = canBreak;
  247.                 else
  248.                     index--;
  249.  
  250.                 theHeight += textHeight;
  251.                 MoveTo ( textRect->left, theHeight );
  252.                 DrawText ( textPtr, startLine+1,
  253.                                     index-startLine-1 );
  254.                 strLength = 0;
  255.                 startLine = canBreak = index;
  256.             } else if ( textPtr[index] EQ ' ' OR
  257.                         textPtr[index] EQ TAB )
  258.                 canBreak = index;
  259.         }
  260.     } /* for index loop */
  261.     
  262.     theHeight += textHeight;
  263.     MoveTo ( textRect->left, theHeight );
  264.     DrawText ( textPtr, (short) (startLine+1), (short) (index-startLine-1) );
  265.  
  266. } /* myTextBox */
  267.  
  268. drawAPicture ( textWindow, dispRect, thePicture )
  269. WindowPtr        textWindow;
  270. Rect            *dispRect;
  271. PicHandle        thePicture;
  272. {
  273.     Rect    drawRect;
  274.     
  275.     drawRect = (*thePicture)->picFrame;
  276.     OffsetRect ( &drawRect, - drawRect.left + dispRect->left,
  277.                             - drawRect.top + dispRect->top );
  278.     DrawPicture ( thePicture, &drawRect );
  279. } /* drawAPicture */
  280.  
  281. drawACenteredPicture ( textWindow, dispRect, thePicture )
  282. WindowPtr        textWindow;
  283. Rect            *dispRect;
  284. PicHandle        thePicture;
  285. {
  286.     Rect    drawRect;
  287.     short    hMove, vMove;
  288.     
  289.     drawRect = (*thePicture)->picFrame;
  290.     hMove = dispRect->left - drawRect.left;
  291.     hMove += dispRect->right - drawRect.right;
  292.     hMove /= 2;
  293.     vMove = dispRect->top - drawRect.top;
  294.     vMove += dispRect->bottom - drawRect.bottom;
  295.     vMove /= 2;
  296.     OffsetRect ( &drawRect, hMove, vMove );
  297.     DrawPicture ( thePicture, &drawRect );
  298. } /* drawACenteredPicture */
  299.  
  300.